<template>
  <c-view>
    <template v-slot:header>
      <div class="title">
        {{ $route.meta.title }}
        <el-button type="primary" :size="size" icon="el-icon-plus" @click="showDialog">新增计费规则</el-button>
      </div>
    </template>
    <div class="main__box">
      <c-table
        ref="cTable"
        selection
        hasBorder
        :max-height="maxHeight"
        :size="size"
        :loading="isLoading"
        :table-header="tableHeader"
        :table-list="tableList"
        :table-inner-btns="tableInnerBtns"
        :page-info="pageInfo"
        @change-pagination="changePagination"
      >
        <template v-slot:header>
          <c-search :form-model="searchObj" :form-items="searchItems" @submit-form="searchSubmit" @reset-form="searchReset"></c-search>
        </template>
      </c-table>
    </div>
    <div v-if="dialogObj.isShow">
      <c-dialog
        class="dialog-box"
        :is-show="dialogObj.isShow"
        :title="dialogObj.title"
        close-btn
        @before-close="dialogObj.isShow = false"
        @on-submit="dialogConfirm"
      >
        <share-add ref="childRef" :init-data.sync="dialogObj.initData" :is-edit="dialogObj.isEdit"></share-add>
      </c-dialog>
    </div>
  </c-view>
</template>

<script>
import mixinTable from 'mixins/table'
import CDialog from 'components/dialog'
import utils from 'utils'
// import dictObj from '@/store/dictData'
import ShareAdd from './add'

export default {
  name: 'chargingShare',
  mixins: [mixinTable],
  components: {
    CDialog,
    ShareAdd
  },
  data(vm) {
    return {
      // 对话框对象
      dialogObj: {},
      // 表格内操作按钮
      tableInnerBtns: [
        {
          name: '编辑',
          icon: 'el-icon-edit',
          handle(row) {
            const {
              id,
              cityCode,
              name,
              timeMoney,
              timeUnit,
              milesMoney,
              status,
              carModelId,
              insuranceMoney,
              maxInsuranceMoney,
              nightTimeUnit,
              nightMilesUnit,
              minExpenditure,
              maxExpenditure,
              otherCost,
              describe,
              deposit
            } = row
            vm.showDialog({
              title: '编辑时租计费规则',
              isEdit: true,
              initData: {
                id,
                cityCode,
                name,
                timeMoney,
                timeUnit,
                milesMoney,
                status,
                carModelId,
                insuranceMoney: insuranceMoney / 100,
                maxInsuranceMoney: maxInsuranceMoney / 100,
                nightTimeUnit,
                nightMilesUnit,
                minExpenditure: minExpenditure / 100,
                maxExpenditure: maxExpenditure / 100,
                otherCost: otherCost / 100,
                describe,
                deposit: deposit / 100
              }
            })
          }
        }
      ],
      tableHeader: [
        {
          label: '所属城市',
          prop: 'cityName'
        },
        {
          label: '所属企业',
          prop: 'companyName'
        },
        {
          label: '车辆类型',
          prop: 'modelName'
        },
        {
          label: '车辆押金(元)',
          prop: 'deposit',
          formatter: row => row && utils.formatMoney(row.deposit)
        },
        {
          label: '计费策略名称',
          prop: 'name',
          width: 110,
          search: {
            type: 'input'
          }
        },
        {
          label: '时间计费(元)',
          prop: 'timeMoney',
          formatter: row => row && utils.formatMoney(row.timeMoney)
        },
        {
          label: '时间计费单元',
          prop: 'timeUnit',
          width: 110
        },
        {
          label: '里程计费(元)',
          prop: 'milesMoney',
          formatter: row => row && utils.formatMoney(row.milesMoney)
        },
        {
          label: '夜间时间计费(元)',
          prop: 'nightTimeUnit',
          width: 130,
          formatter: row => row && utils.formatMoney(row.nightTimeUnit)
        },
        {
          label: '夜间里程计费(元)',
          prop: 'nightMilesUnit',
          width: 130,
          formatter: row => row && utils.formatMoney(row.nightMilesUnit)
        },
        {
          label: '不计免赔价格(元)',
          prop: 'insuranceMoney',
          width: 130,
          formatter: row => row && utils.formatMoney(row.insuranceMoney)
        },
        {
          label: '24H不计免赔金额(元)',
          prop: 'maxInsuranceMoney',
          width: 160,
          formatter: row => row && utils.formatMoney(row.maxInsuranceMoney)
        },
        // {
        //   label: '是否启用',
        //   prop: 'status',
        //   width: 100,
        //   formatter: row => row && vm.setTableColumnLabel(row.status, 'disabledStatus'),
        //   search: {
        //     type: 'dict',
        //     optionsList: dictObj.disabledStatus
        //   }
        // },
        {
          label: '最低消费(元)',
          prop: 'minExpenditure',
          formatter: row => row && utils.formatMoney(row.minExpenditure)
        },
        {
          label: '最高消费(元)',
          prop: 'maxExpenditure',
          formatter: row => row && utils.formatMoney(row.maxExpenditure)
        },
        {
          label: '其他费用(元)',
          prop: 'otherCost',
          formatter: row => row && utils.formatMoney(row.otherCost)
        },
        {
          label: '描述',
          prop: 'describe',
          inline: true
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    /*
     * 查询表格列表数据
     */
    fetchData() {
      const { pageNo, pageSize } = this.pageInfo
      const { dateTime, ...other } = this.searchObj
      const searchDate = this.getSearchDate(dateTime)
      this.isLoading = true
      this.$api.settings
        .fetch('timeshareRateList', {
          ...searchDate,
          ...other,
          rows: pageNo,
          offset: pageSize
        })
        .then(res => {
          this.isLoading = false
          if (res && res.totalCount) {
            const { data, totalCount } = res
            this.pageInfo.totalNum = totalCount
            this.tableList = data || []
          } else {
            this.tableList = res || []
          }
        })
    },
    /**
     * dialog对话框数据处理
     * @opts {*}
     */
    showDialog(opts) {
      this.dialogObj = {
        isShow: true,
        isEdit: opts.isEdit || false,
        title: opts.title || '新增时租计费规则',
        initData: opts.initData
      }
    },
    /**
     * dialog新增、编辑处理函数
     */
    dialogConfirm() {
      const childRef = this.$refs.childRef
      childRef.$refs.formRef.validate(valid => {
        if (valid) {
          const params = childRef.formModel
          const { insuranceMoney, maxInsuranceMoney, minExpenditure, maxExpenditure, otherCost, deposit, ...other } = params
          // TODO...
          this.$api.settings
            .fetch('timeshareRateSaveOrUpdate', {
              insuranceMoney: insuranceMoney * 100,
              maxInsuranceMoney: maxInsuranceMoney * 100,
              minExpenditure: minExpenditure * 100,
              maxExpenditure: maxExpenditure * 100,
              otherCost: otherCost * 100,
              deposit: deposit * 100,
              ...other
            })
            .then(() => {
              this.responeHandle(this.dialogObj.isEdit ? '更新成功' : '新增成功')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
